import show_toast from '@dfeidao/atom-wxapp//api/interaction/show-toast';
import get_global from '@dfeidao/atom-wxapp//global/get';
import set_global from '@dfeidao/atom-wxapp//global/set';
import hide_loading from '@dfeidao/atom-wxapp/api/interaction/hide-loading';
import show_modal from '@dfeidao/atom-wxapp/api/interaction/show-modal';
import { IFeidaoAiWeappPage } from '@dfeidao/atom-wxapp/interfaces';
import get_local from '@dfeidao/atom-wxapp/local/get';
import nodejs from '@dfeidao/atom-wxapp/msg/nodejs';
export default async function a005(fd: IFeidaoAiWeappPage) {
	const data = get_local(fd, 'data') as Array<{ number_storage: number }>;
	const save_data = data.filter((i) => {
		return i.number_storage !== 0;
	});
	// console.log(save_data);

	const userinfo = get_global('userinfo');
	const doccode = get_global('doccode');
	if (save_data.length === 0) {
		show_toast('没有可以保存的数据', 'none', 1000);
		return;
	}

	try {
		// app-supply-det-save
		const data1 = await nodejs<{ doccode: string, msg: string }>('app-supply-det-save', {
			dataArray: save_data,
			userInfo: {
				user_code: userinfo.usercode
			},
			doccode: doccode || '',
			systemid: '6641'
		});

		// console.log(data, '--------data---------');
		if (data1.msg === 'success') {
			set_global('doccode', data1.doccode);
			// 过账
			// bill(fd);
		}
	} catch (error) {
		// console.log('---------err---------');
		const succ = await show_modal('保存失败', error.errMsg.toString());
		if (succ.msg.confirm) {
			// console.log('用户点击确定');
		} else if (succ.msg.cancel) {
			// console.log('用户点击取消');
		}
		hide_loading();
	}

}
